package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.PlaySrcBean;
import com.elinkway.infinitemovies.bean.PlaySrcList;
import com.elinkway.infinitemovies.config.LogoImageLoader;
import com.le123.ysdq.R;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

public class PlaySrcPopupListAdapter extends MyBaseAdapter {

	private PlaySrcList playsrcs;

	private String mCurrentSite;
	
	private class ItemHolder {
		ImageView icon;
		TextView name;
		ProgressBar mProgressBar;
	}

	public PlaySrcPopupListAdapter(Context context,
			PlaySrcList playsrcs) {
		super(context);
		this.playsrcs = playsrcs;
	}

	@Override
	public int getCount() {
		return playsrcs.getPlaySrcList().size();
	}

	@Override
	public Object getItem(int position) {
		return position;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {

		ItemHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.play_src_choice_item, null);
			holder = new ItemHolder();
			holder.icon = (ImageView) convertView
					.findViewById(R.id.play_src_choice_logo);
			holder.name = (TextView) convertView
					.findViewById(R.id.play_src_choice_name);
			holder.mProgressBar = (ProgressBar) convertView.findViewById(R.id.play_src_choice_logo_progress);
			convertView.setTag(holder);
		} else {
			holder = (ItemHolder) convertView.getTag();
		}
		LogoImageLoader.getInstance().displayImage(playsrcs.getPlaySrcList().get(position).getLogo(), holder.icon, new ItemImageLoadingListener(holder.mProgressBar
				));
		holder.name.setText(playsrcs.getPlaySrcList().get(position).getSitename());
		holder.name.setTextColor(mContext.getResources().getColor(R.color.color_666666));
		return convertView;
	}
	private class ItemImageLoadingListener implements ImageLoadingListener {
		
		private ProgressBar pb;
		
		public ItemImageLoadingListener(ProgressBar pb) {
			super();
			this.pb = pb;
		}

		@Override
		public void onLoadingStarted(String arg0, View arg1) {
		}
		
		@Override
		public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
			pb.setVisibility(View.GONE);
		}
		
		@Override
		public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
			pb.setVisibility(View.GONE);
		}
		
		@Override
		public void onLoadingCancelled(String arg0, View arg1) {
			pb.setVisibility(View.GONE);
		}
	};
	
	public void setList(PlaySrcList playsrcs){
		this.playsrcs = playsrcs;
		notifyDataSetChanged();
	}

	public void setList(PlaySrcList playsrcs, String site){
		PlaySrcList playsrcsTemp = new PlaySrcList();
		if(playsrcs != null && playsrcs.getPlaySrcList().size() > 0){
			for(int i = 0; i < playsrcs.getPlaySrcList().size(); i++){
				PlaySrcBean mPlaySrcBean = playsrcs.getPlaySrcList().get(i);
				if(site != null && !site.equals(mPlaySrcBean.getSite())){
					playsrcsTemp.getPlaySrcList().add(mPlaySrcBean);
				}
			}
		}
		this.playsrcs = playsrcsTemp;
		notifyDataSetChanged();
	}

	public PlaySrcList getPlaySrcList(){
		return playsrcs;
	}

	public void setSelectPosition(String site) {
		if (site.equals(mCurrentSite)) {
			return;
		}
		mCurrentSite = site;
		notifyDataSetChanged();
	}

}
